package com.personal.service.impl;

import com.personal.dao.IndustryDao;
import com.personal.domain.Industry;
import com.personal.domain.obtain.IndustryDireTree;
import com.personal.service.IndustryService;
import com.personal.util.partDisplay.IndustryPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: smartCommunity_springboot
 * @package: com.personal.service.impl
 * @className: IndustryServiceImpl
 * @author: bong~
 * @description: TODO
 * @date: 2023/8/11 11:42
 * @version: 1.0
 */

@Service
public class IndustryServiceImpl implements IndustryService {

    @Autowired
    private IndustryDao industryDao;
    @Override
    public List<Industry> selectAll() {
        List<Industry> list=industryDao.selectAll();
        return list;
    }

    @Override
    public List<IndustryPart> selectPartAll() {
        List<IndustryPart> list=industryDao.selectPartAll();
        return list;
    }

    //点击目录树，获取industryId的list
    @Override
    public List<IndustryPart> selectPartByIds(List<Integer> industryIds) {
        List<IndustryPart> list=new ArrayList<>();
        for(int industryId:industryIds){
            IndustryPart cur=industryDao.selectPartById(industryId);
            list.add(cur);
        }
        return list;
    }

    @Override
    public List<IndustryPart> selectPartByKey(String name) {
        List<IndustryPart> list=industryDao.selectPartByKey(name);
        return list;
    }

    @Override
    public boolean add(Industry industry) {
        int flag= industryDao.add(industry);
        if(flag>0)
            return true;
        return false;
    }

    @Override
    public Industry getUpdateData(int industryId) {
        Industry cur=industryDao.getUpdateData(industryId);
        return cur;
    }

    @Override
    public boolean update(Industry industry) {
        int flag=industryDao.update(industry);
        if(flag>0)
            return true;
        return false;
    }

    @Override
    public boolean delete(int industryId) {
        int flag=industryDao.delete(industryId);
        if(flag>0)
            return true;
        return false;
    }

    @Override
    public List<IndustryDireTree> getDirectory(Integer parentId) {

        List<IndustryDireTree> directories=new ArrayList<>();
        List<Industry> list=new ArrayList<>();

        list=industryDao.selectByParentId(parentId);

        for(Industry cur:list){
            int industryId=cur.getIndustryId();
            int curParentId=cur.getParentId();
            String industryCategoryName=cur.getIndustryCategoryName();
            int forbidden=cur.getForbidden();
            String remark=cur.getRemark();
            String path=cur.getPath();

            IndustryDireTree directory=new IndustryDireTree(industryId,curParentId,industryCategoryName,forbidden,remark,path);
            List<IndustryDireTree> subDirectory=getDirectory(industryId);
            directory.setSubDirectory(subDirectory);

            directories.add(directory);

        }
        return directories;
    }
}
